exports.mongoose = () => {
  const mongoose = require("mongoose");
  mongoose.connect("mongodb://localhost:27017/koa");

  //创建规则
  //   const Schema = mongoose.Schema;
  //   const students = new Schema({
  //     name: {
  //       type: String,
  //     },
  //     age: {
  //       type: Number,
  //     },
  //     sex: {
  //       type: String,
  //     },
  //   });

  //创建模型 创建表 第一个参数是表的名字
  //   const Students = mongoose.model("students", students);
  //   const db = new Students({ name: "zs", age: "19", sex: "男" });
  //   db.save();

  //创建规则
  const Schema = new mongoose.Schema({
    name: {
      type: String,
    },
    age: {
      type: Number,
    },
    sex: {
      type: String,
    },
  });

  //创建模型 创建表 第一个参数是表的名字
  const Students = mongoose.model("students", Schema);

  //创建增
  /*  new Students({ name: "zs", age: "19", sex: "男" }).save((err, data) => {
    console.log(data);
  }); */

  /* new Students({ name: "ls", age: "19", sex: "男" })
    .save()
    .then((res) => console.log(res))
    .catch((err) => console.log(err)); */

  Students.create({ name: "ls", age: "29", sex: "男" })
    .then((res) => console.log(res))
    .catch((err) => console.log(err));

  /*   Students.findOne().then((res) => {
    console.log(res);
  }); */

  /*  Students.deleteOne({ name: "zs" })
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */

  /* Students.deleteMany({ name: "zs" })
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */

  /* Students.deleteMany()
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */

  /*   Students.updateOne({ name: "zs" }, { name: "NB" })
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */

  /*  Students.findOneAndUpdate(
    { _id: "612c92e94e414110f0693db0" },
    { name: "zs", age: "99" }
  )
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */

  /*  Students.updateMany({ age: 99 }, { age: 19 })
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    }); */
};
